package com.yuma.tingshu.order.template;

import com.yuma.tingshu.album.client.AlbumInfoFeignClient;
import com.yuma.tingshu.album.client.TrackInfoFeignClient;
import com.yuma.tingshu.common.constant.SystemConstant;
import com.yuma.tingshu.common.execption.YumaException;
import com.yuma.tingshu.common.result.Result;
import com.yuma.tingshu.common.result.ResultCodeEnum;
import com.yuma.tingshu.model.album.AlbumInfo;
import com.yuma.tingshu.model.album.TrackInfo;
import com.yuma.tingshu.order.template.anno.TradBean;
import com.yuma.tingshu.vo.order.OrderDetailVo;
import com.yuma.tingshu.vo.order.OrderInfoVo;
import com.yuma.tingshu.vo.order.TradeVo;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@TradBean(value = SystemConstant.ORDER_ITEM_TYPE_TRACK)
public class TrackTrade extends TradeTemplate{

    @Resource
    private AlbumInfoFeignClient albumInfoFeignClient;

    @Resource
    private TrackInfoFeignClient trackInfoFeignClient;

    @Override
    public void buildOrderInfoVo(OrderInfoVo orderInfoVo, TradeVo tradeVo) {
        //----------4.3 声音订单
        // 1.判空 判断值 > 0
        if(tradeVo.getTrackCount() == null || tradeVo.getTrackCount() <= 0){
            // 1.1为空抛异常
            throw new YumaException(ResultCodeEnum.ARGUMENT_VALID_ERROR);
        }
        // 1.2查询本次购买声音列表(album模块远程调用)
        Result<List<TrackInfo>> needBuyListResult = trackInfoFeignClient.findTradeTrackList(tradeVo.getItemId(), tradeVo.getTrackCount());
        // 2.远程调用后断言
        Assert.notNull(needBuyListResult, "声音详情的订单页面，根据声音id查询声音列表失败");
        List<TrackInfo> needBuyList = needBuyListResult.getData();
        Assert.notNull(needBuyList, "声音详情的订单页面，根据声音id查询声音列表失败");
        // 3.根据声音求专辑Id
        Long albumId = needBuyList.get(0).getAlbumId();
        // 4.根据专辑id查询专辑信息
        Result<AlbumInfo> albumInfoResult = albumInfoFeignClient.getAlbumInfo(albumId);
        Assert.notNull(albumInfoResult, "声音详情的订单页面，根据专辑id查询专辑信息失败");
        AlbumInfo albumInfo = albumInfoResult.getData();
        Assert.notNull(albumInfo, "声音详情的订单页面，根据专辑id查询专辑信息失败");
        // 5.封装订单三个价格,声音没有折扣
        orderInfoVo.setOriginalAmount(albumInfo.getPrice().multiply(BigDecimal.valueOf(needBuyList.size())).setScale(2));
        orderInfoVo.setOrderAmount(orderInfoVo.getOriginalAmount().setScale(2));
        orderInfoVo.setDerateAmount(BigDecimal.ZERO.setScale(2));
        // 6.封装订单的详细集合
        orderInfoVo.setOrderDetailVoList(
                needBuyList.stream().map(trackInfo ->
                        new OrderDetailVo()
                                .setItemId(trackInfo.getId())
                                .setItemName(trackInfo.getTrackTitle())
                                .setItemPrice(albumInfo.getPrice().setScale(2))
                                .setItemUrl(trackInfo.getCoverUrl())
                ).collect(Collectors.toList())
        );
        // 不需要封装订单减免集合，声音不打折
    }
}
